package com.djz.stuinfomge.dao.impl;

import com.djz.stuinfomge.dao.IAchievementDAO;
import com.djz.stuinfomge.entity.Achievement;
import com.djz.stuinfomge.entity.PageBean;
import com.djz.stuinfomge.entity.Student;
import com.djz.stuinfomge.exception.PageNumOutOfRange;
import com.djz.stuinfomge.util.Finder;

import java.util.List;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.criterion.Example;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Repository;

/**
 * A data access object (DAO) providing persistence and search support for
 * Achievement entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.djz.stuinfomge.entity.Achievement
 * @author MyEclipse Persistence Tools
 */
@Repository
public class AchievementDAO extends BaseDaoImpl<Achievement> implements
		IAchievementDAO {
	private static final Logger log = LoggerFactory
			.getLogger(AchievementDAO.class);

	public PageBean<Achievement> getPage(Integer pageNo, Integer size,
			Achievement bean) throws PageNumOutOfRange {
		// 根据条件构造JPA-QL
		Finder finder = Finder.create("from Achievement");

		finder.append("where 1=1");

		if (bean != null && bean.getStudent() != null
				&& bean.getStudent().getId() > 0) {

			finder.append("and student.id = :studentId").setParam("studentId",
					bean.getStudent().getId());
		}

		if (bean != null && bean.getStudent() != null
				&& bean.getStudent().getName() != null) {

			finder.append("and student.name like :name").setParam("name",
					"%" + bean.getStudent().getName() + "%");
		}

		finder.append("order by id desc");
		log
				.debug("finder: " + finder + " pageNo: " + pageNo + " size: "
						+ size);
		PageBean<Achievement> page = find(finder, pageNo, size);
		return page;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.djz.stuinfomge.dao.impl.IArchievementDAO#save(com.djz.stuinfomge.
	 * entity.Achievement)
	 */
	public void save(Achievement transientInstance) {
		log.debug("saving Achievement instance");
		try {
			getSession().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.djz.stuinfomge.dao.impl.IArchievementDAO#delete(com.djz.stuinfomge
	 * .entity.Achievement)
	 */
	public void delete(Achievement persistentInstance) {
		log.debug("deleting Achievement instance");
		try {
			getSession().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.djz.stuinfomge.dao.impl.IArchievementDAO#findById(java.lang.Integer)
	 */
	public Achievement findById(java.lang.Integer id) {
		log.debug("getting Achievement instance with id: " + id);
		try {
			Achievement instance = (Achievement) getSession().get(
					"com.djz.stuinfomge.entity.Achievement", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.djz.stuinfomge.dao.impl.IArchievementDAO#findByExample(com.djz.stuinfomge
	 * .entity.Achievement)
	 */
	public List findByExample(Achievement instance) {
		log.debug("finding Achievement instance by example");
		try {
			List results = getSession().createCriteria(
					"com.djz.stuinfomge.entity.Achievement").add(
					Example.create(instance)).list();
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.djz.stuinfomge.dao.impl.IArchievementDAO#findByProperty(java.lang
	 * .String, java.lang.Object)
	 */
	public List findByProperty(String propertyName, Object value) {
		log.debug("finding Achievement instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from Achievement as model where model."
					+ propertyName + "= ?";
			Query queryObject = getSession().createQuery(queryString);
			queryObject.setParameter(0, value);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see com.djz.stuinfomge.dao.impl.IArchievementDAO#findAll()
	 */
	public List findAll() {
		log.debug("finding all Achievement instances");
		try {
			String queryString = "from Achievement";
			Query queryObject = getSession().createQuery(queryString);
			return queryObject.list();
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.djz.stuinfomge.dao.impl.IArchievementDAO#merge(com.djz.stuinfomge
	 * .entity.Achievement)
	 */
	public Achievement merge(Achievement detachedInstance) {
		log.debug("merging Achievement instance");
		try {
			Achievement result = (Achievement) getSession().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.djz.stuinfomge.dao.impl.IArchievementDAO#attachDirty(com.djz.stuinfomge
	 * .entity.Achievement)
	 */
	public void attachDirty(Achievement instance) {
		log.debug("attaching dirty Achievement instance");
		try {
			getSession().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * com.djz.stuinfomge.dao.impl.IArchievementDAO#attachClean(com.djz.stuinfomge
	 * .entity.Achievement)
	 */
	public void attachClean(Achievement instance) {
		log.debug("attaching clean Achievement instance");
		try {
			getSession().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

}
